#pragma once

#include "cameraModule/CameraResult.h"
#include <memory>
#include <deque>
#include <mutex>
//#include"Common/cSafeLocker.h"

#define MAX_VFR_LENGTH (5)

#ifdef _WIN32
#include <windows.h>

class MYLocker
{
public:
    MYLocker(CRITICAL_SECTION* inHandle) : m_handle(inHandle)
    {
        if (NULL != m_handle)
        {
            EnterCriticalSection(m_handle);
        }
    }

    ~MYLocker()
    {
        if (NULL != m_handle)
        {
            LeaveCriticalSection(m_handle);
            m_handle = NULL;
        }
    }
private:
    CRITICAL_SECTION* m_handle;
};
#else

//typedef struct ThreadMutex
//{
//    ThreadMutex()
//    {
//        pthread_mutex_init(&mtx, NULL);
//    }
//    ~ThreadMutex()
//    {
//        pthread_mutex_destroy(&mtx);
//    }
//    inline void lock()
//    {

//        pthread_mutex_lock(&mtx);
//        //    printf("Enter %d\n", ++i);
//    }
//    inline void unlock()
//    {
//        pthread_mutex_unlock(&mtx);
//        //     printf("Leave %d\n", ++t);
//    }
//    pthread_mutex_t mtx;
//    //   int i;
//    //   int t;
//}ThreadMutex;

//class MYLocker
//{
//public:
//    MYLocker(ThreadMutex* inHandle) : m_handle(inHandle)
//    {
//        if (NULL != m_handle)
//        {
//            m_handle->lock();
//        }
//    }

//    ~MYLocker()
//    {
//        if (NULL != m_handle)
//        {
//            m_handle->unlock();

//            m_handle = NULL;
//        }
//    }
//private:
//    ThreadMutex* m_handle;
//};

#endif

#define USE_SMART_PTR


namespace libcvos_vfr
{

	class ResultListManager
	{
    public:
#ifdef USE_SMART_PTR
        typedef std::shared_ptr<CameraResult> Result_Type;
#else
        typedef CameraResult* Result_Type;
#endif
		typedef std::deque<Result_Type> list_Type;
	public:
		ResultListManager();
		~ResultListManager();

	public:
		size_t size();
		bool empty();

		void front(Result_Type& result);
		void frontAndPop(Result_Type& result);
		void back(Result_Type& result);
		Result_Type GetOneByIndex(int iPosition);
		Result_Type GetOneByCarid(unsigned long dwCarID);
		Result_Type GetOneByPlateNumber(const char *plateNo);
		Result_Type GetOneByListNo(const char *ListNo);
		void pop_front();
		void pop_back();
		void push_back(Result_Type result);

		int GetPositionByPlateNo(const char* plateNo);
		void DeleteToPosition(std::string::size_type position);
		void DeleteByCarID(unsigned long dwCarID);
		bool ReplaceByCarID(unsigned long dwCarID, Result_Type result);

		std::string GetAllPlateString();
		std::string GetAllVehTypeString();

		void ClearALLResult();


	private:
		list_Type m_list;
		//std::mutex m_mtx;
		//#ifdef _WIN32
		//    CRITICAL_SECTION m_hcs;
		//#else
		//    ThreadMutex m_hcs;
		//#endif

		//ThreadMutex m_hcs;
		std::mutex m_hcs;

		friend class MYLocker;
	};

}
